package com.health.activity.photo;

import java.io.File;

import com.health.R;
import com.health.tools.GlobalConts;
import com.nova.NovaConfig;
import com.nova.clipimage.ClipImageLayout;
import com.nova.tools.ImageUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 裁剪页面
 */
public class ClipActivity extends Activity {
	private ClipImageLayout mClipImageLayout;
	private String path;
	private ProgressDialog loadingDialog;

	private TextView title;
	private Button btn_up, btn_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_clipimage);
		try {

			// 这步必须要加
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			loadingDialog = new ProgressDialog(this);
			loadingDialog.setTitle("请稍后...");
			path = getIntent().getStringExtra("str_img");
			if (TextUtils.isEmpty(path) || !(new File(path).exists())) {
				Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
				return;
			}
			Bitmap bitmap = ImageUtil.convertToBitmap(path, 600, 600);
			int degree = ImageUtil.readPictureDegree(path);
			bitmap = ImageUtil.rotaingImageView(degree, bitmap);
			if (bitmap == null) {
				Toast.makeText(this, "图片加载失败", Toast.LENGTH_SHORT).show();
				return;
			}

			initView();
			addListeners();
			NovaConfig.photo_flag = GlobalConts.photo_flag;
			mClipImageLayout = (ClipImageLayout) findViewById(R.id.id_clipImageLayout);
			mClipImageLayout.setBitmap(bitmap);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initView() {
		btn_up = (Button) findViewById(R.id.btn_up);
		btn_up.setText(getResources().getString(R.string.save));
		btn_up.setBackground(null);
		title = (TextView) findViewById(R.id.title);
		title.setText(getResources().getString(R.string.clipimage));
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_back.setVisibility(View.GONE);
	}

	private void addListeners() {
		btn_up.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				loadingDialog.show();
				new Thread(new Runnable() {
					@Override
					public void run() {
						Bitmap bitmap = mClipImageLayout.clip();
						String path = Environment.getExternalStorageDirectory() + GlobalConts.IMG_FOLDERS + "/"
								+ System.currentTimeMillis() + ".png";
						if(!new File( Environment.getExternalStorageDirectory() + GlobalConts.IMG_FOLDERS + "").exists()){
							new File( Environment.getExternalStorageDirectory() + GlobalConts.IMG_FOLDERS + "").mkdirs();
						}
						ImageUtil.savePhotoToSDCard(bitmap, path);
						loadingDialog.dismiss();
						Intent intent = new Intent();
						intent.putExtra("path", path);
						setResult(RESULT_OK, intent);
						// InformationChange.setImgPath(path);
						finish();
					}
				}).start();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
